import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iyouqu.dycm.base.bean.AdType;
import com.iyouqu.dycm.base.bean.constants.EnumOnOff;
import com.iyouqu.dycm.base.service.AdTypeService;
import com.iyouqu.dycm.sched.bean.MarkType;
import com.iyouqu.dycm.sched.bean.MediaAppType;
import com.iyouqu.dycm.sched.bean.PlatformType;
import com.iyouqu.dycm.sched.bean.SchedConfig;
import com.iyouqu.dycm.sched.bean.SchedSplitPageBean;
import com.iyouqu.dycm.sched.service.SchedConfigService;
	
	@Autowired
	private SchedConfigService schedConfigService;
	
	@Autowired
	private AdTypeService adTypeService;
	
	private String list = "/sche/list";
	private String edit = "/sche/edit";
	private String patchEdit = "/sche/patchEdit";
	private String reList = "redirect:" + list;
	private String addAction = "/sche/add";
	private String uptAction = "/sche/upt";
	private String delAction = "/sche/del";
	private String patchAdd = "/sche/patchAdd";
	private String patchAddTitle = "批量增加";
	private String StringKey = "调度";
	private String addTitle = "增加" + StringKey;
	private String uptTitle = "修改" + StringKey;
	private String listHead = StringKey + "管理";

	// list get
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model m) {
		m.addAttribute("addAction", addAction);
		m.addAttribute("uptAction", uptAction);
		m.addAttribute("delAction", delAction);
		m.addAttribute("listHead", listHead);
		m.addAttribute("uptTitle", uptTitle);
		m.addAttribute("addTitle", addTitle);
		m.addAttribute("list", list);
		m.addAttribute("edit", edit);
		m.addAttribute("reList", reList);
		m.addAttribute("patchAdd", patchAdd);
		m.addAttribute("patchAddTitle", patchAddTitle);
		return list;
	}

	// list post
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(@RequestParam(value = "s", required = false, defaultValue = "1") int start,
			@RequestParam(value = "p", required = false, defaultValue = "10") int perPage,
			@RequestParam(value = "k", required = false) String appid) {
		// 返回参数
		SchedSplitPageBean<SchedConfig> spb = null;
		if (notEmpty(appid)) {
			spb = schedConfigService.getAllPlatAppInfo(appid, start, perPage);
		} else {
			spb = schedConfigService.getAllPlatAppInfo(null, start, perPage);
		}

		Long count = spb.getRowCount();
		Long tmp = count / perPage;
		Long pages = count % perPage == 0 ? tmp : tmp + 1;
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("datas", spb.getDatas());// 数据
		json.put("pages", pages);// 总页
		json.put("start", start);// 页码
		json.put("count", count);// 总数
		return json;
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("action", addAction);
		model.addAttribute("title", addTitle);
		SchedConfig sc = new SchedConfig();
		model.addAttribute("m", sc);

		model.addAttribute("mapps", mediaAppTypeService.query());
		model.addAttribute("plats", platformTypeService.query());
		model.addAttribute("marks", markTypeService.query());

		return edit;
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(SchedConfig sc, Model model) {
		model.addAttribute("action", addAction);
		model.addAttribute("title", addTitle);
		model.addAttribute("m", sc);
		
		// 验证
		MarkType mt = markTypeService.queryOne(sc.getMark_id());
		sc.setMark_name(mt.getMark_desc());

		MediaAppType mat = mediaAppTypeService.queryOne(sc.getApp_package());
		sc.setApp_name(mat.getApp_name());

		
		PlatformType pt = platformTypeService.queryOne(sc.getPlatform_id());
		
		sc.setPlatform_name(pt.getPlatform_name());

		if (empty(sc.getVersion())) {
			model.addAttribute("error", "版本不为空");
			return edit;
		}

		if (empty(sc.getApp_id())) {
			model.addAttribute("error", "三方平台APPID不为空");
			return edit;
		}
		if (empty(sc.getAdcode())) {
			model.addAttribute("error", "广告代码位不为空");
			return edit;
		}

		sc.setId(null);
		sc.setStatus(EnumOnOff.OFF.getKey());
		sc.setCreate_time(new Date().getTime());
		sc.setUpdate_time(new Date().getTime());
		schedConfigService.addSchedConfig(sc);
		return reList;
	}

	@RequestMapping(value = "upt/{id}", method = RequestMethod.GET)
	public String upt(@PathVariable("id") String id, Model model) {
		model.addAttribute("action", uptAction);
		model.addAttribute("title", uptTitle);
		SchedConfig sc = schedConfigService.getPlatAppInfoById(id);
		model.addAttribute("m", sc);

		model.addAttribute("mapps", mediaAppTypeService.query());
		model.addAttribute("plats", platformTypeService.query());
		model.addAttribute("marks", markTypeService.query());

		return edit;
	}

	@RequestMapping(value = "upt", method = RequestMethod.POST)
	public String upt(SchedConfig sc, Model model) {
		model.addAttribute("action", uptAction);
		model.addAttribute("title", uptTitle);
		model.addAttribute("m", sc);

		// 验证
		MarkType mt = markTypeService.queryOne(sc.getMark_id());
		sc.setMark_name(mt.getMark_desc());

		MediaAppType mat = mediaAppTypeService.queryOne(sc.getApp_package());
		sc.setApp_name(mat.getApp_name());

		PlatformType pt = platformTypeService.queryOne(sc.getPlatform_id());
		sc.setPlatform_name(pt.getPlatform_name());

		if (empty(sc.getVersion())) {
			model.addAttribute("error", "版本不为空");
			return edit;
		}

		if (empty(sc.getApp_id())) {
			model.addAttribute("error", "三方平台APPID不为空");
			return edit;
		}
		if (empty(sc.getAdcode())) {
			model.addAttribute("error", "广告代码位不为空");
			return edit;
		}

		schedConfigService.updateSchedConfig(sc);
		return reList;
	}

	@RequestMapping(value = "del/{id}", method = RequestMethod.GET)
	public String del(@PathVariable("id") String id, Model model) {
		model.addAttribute("action", delAction);
		model.addAttribute("title", uptTitle);
		SchedConfig sc = new SchedConfig();
		sc.setId(id);
		schedConfigService.deleteSchedConfig(sc);
		model.addAttribute("m", sc);
		return reList;
	}

	@RequestMapping(value = "lock", method = RequestMethod.GET)
	public String lock(@RequestParam("id") String id, @RequestParam("s") Integer s) {
		SchedConfig sc = new SchedConfig();
		sc.setId(id);
		sc.setStatus(s);
		schedConfigService.updateSchedConfig(sc);
		return reList;
	}

	@RequestMapping(value = "/patchAdd", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> patchAdd(@RequestBody List<SchedConfig> scs) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (scs != null && scs.size() > 0) {
			for (int i = 0; i < scs.size(); i++) {
				SchedConfig sc = scs.get(i);
				sc.setId(null);
				sc.setCreate_time(new Date().getTime());
				sc.setUpdate_time(new Date().getTime());
				schedConfigService.addSchedConfig(sc);
			}
		}
		map.put("message", "true");
		return map;
	}

	@RequestMapping(value = "patchAdd", method = RequestMethod.GET)
	public String patchAdd(Model model) {
		model.addAttribute("action", patchAdd);
		model.addAttribute("title", patchAdd);
		SchedConfig sc = new SchedConfig();
		model.addAttribute("m", sc);
		List<AdType> list = adTypeService.queryAdType();
		model.addAttribute("adtyps", list);
		return patchEdit;
	}
